package sg.org.scs.splashawards.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.SessionAware;

import sg.org.scs.splashawards.biz.TeamService;
import sg.org.scs.splashawards.dto.Participation;
import sg.org.scs.splashawards.dto.Student;
import sg.org.scs.splashawards.dto.Team;
import sg.org.scs.splashawards.dto.TeamAttachment;
import sg.org.scs.splashawards.exceptions.DAOException;
import sg.org.scs.splashawards.exceptions.DataFormatException;
import sg.org.scs.splashawards.exceptions.DuplicationException;
import sg.org.scs.splashawards.utils.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class RegisterAction extends ActionSupport implements SessionAware {

	private List<String> level = Constants.SCHOOL_LEVEL;
	private List<String> topic = Constants.TOPIC;
	private List<File> attachmentFile;
	private List<String> attachmentFileContentType;
	private List<String> attachmentFileFileName;

	private List<String> studentName;
	private List<String> studentEmail;
	private List<String> studentMobile;
	private List<String> studentNric;
	
	private List<String> type = Constants.PARTICIPATION;
	
	private Map<String,Object> session;
	
	private String selectedType;

	public String getSelectedType() {
		return selectedType;
	}

	public void setSelectedType(String selectedType) {
		this.selectedType = selectedType;
	}

	public List<String> getType() {
		return type;
	}

	public void setType(List<String> type) {
		this.type = type;
	}
	
	public String getDefaultType(){
		return (Constants.PARTICIPATION.get(1));
	}

	public List<String> getStudentName() {
		return studentName;
	}

	public void setStudentName(List<String> studentName) {
		this.studentName = studentName;
	}

	public List<String> getStudentEmail() {
		return studentEmail;
	}

	public void setStudentEmail(List<String> studentEmail) {
		this.studentEmail = studentEmail;
	}

	public List<String> getStudentMobile() {
		return studentMobile;
	}

	public void setStudentMobile(List<String> studentMobile) {
		this.studentMobile = studentMobile;
	}

	public List<String> getStudentNric() {
		return studentNric;
	}

	public void setStudentNric(List<String> studentNric) {
		this.studentNric = studentNric;
	}

	private Team team;
	private TeamService service = new TeamService();

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public List<String> getTopic() {
		return topic;
	}

	public void setTopic(List<String> topic) {
		this.topic = topic;
	}

	public List<String> getLevel() {
		return level;
	}

	public void setLevel(List<String> level) {
		this.level = level;
	}

	public String showRegister() {
		return SUCCESS;
	}

	public String register() {
		List<TeamAttachment> teamattachment = new ArrayList<TeamAttachment>();
		try {
			System.out.println("asadadasda");
			if(!attachmentFile.isEmpty()){
			for (int i = 0; i < attachmentFile.size(); i++) {
				File uploadedFile = new File(Constants.filePathLocation+"/"+getTeam().getTeamName(),
						this.attachmentFileFileName.get(i));
				FileUtils.copyFile(this.attachmentFile.get(i), uploadedFile);
				System.out.println("asadadasda");
				TeamAttachment attachment = new TeamAttachment();
				System.out.println(getTeam().getTeamName());
				attachment.setFileLocation(Constants.filePathLocation+"/"+getTeam().getTeamName()+"/"
						+ this.attachmentFileFileName.get(i));
				teamattachment.add(attachment);
			}
			}
			else {
				System.out.println("Hellow");
				addActionMessage("Please upload your attachments");
				return INPUT;
			}
			getTeam().setAttachment(teamattachment);
			getTeam().setStudent(getStudentList());
			getTeam().setParticipation(getParticipation());
			service.createTeam(getTeam());
			//session.put("team", service.getTeam(teamid));
			
		} catch (DAOException e) {
			addActionMessage(e.getMessage());
			return INPUT;
		} catch (DataFormatException e) {
			addActionMessage(e.getMessage());
			return INPUT;
		} catch (DuplicationException e) {
			addActionMessage(e.getMessage());
			return INPUT;
		} catch (IOException e) {
			addActionMessage("Error Uploading file.Please try again");
			return INPUT;
		} catch (Exception e) {
			addActionMessage("Error Uploading file.Please try again");
			return INPUT;
		}
		return SUCCESS;
	}

	public List<File> getAttachmentFile() {
		return attachmentFile;
	}

	public void setAttachmentFile(List<File> attachmentFile) {
		this.attachmentFile = attachmentFile;
	}

	public List<String> getAttachmentFileContentType() {
		return attachmentFileContentType;
	}

	public void setAttachmentFileContentType(
			List<String> attachmentFileContentType) {
		this.attachmentFileContentType = attachmentFileContentType;
	}

	public List<String> getAttachmentFileFileName() {
		return attachmentFileFileName;
	}

	public void setAttachmentFileFileName(List<String> attachmentFileFileName) {
		this.attachmentFileFileName = attachmentFileFileName;
	}

	public String attachmentUpload() {
		for (File file : attachmentFile) {
			System.out.println("File :" + file);
		}

		for (String fileName : attachmentFileFileName) {
			System.out.println("Filename : " + fileName);
		}

		for (String contentType : attachmentFileContentType) {
			System.out.println("File type : " + contentType);
		}
		return SUCCESS;
	}

	private List<Student> getStudentList() {
		List<Student> students = new ArrayList<Student>();

		for (int i = 0; i < studentName.size(); i++) {
			Student student = new Student();
			student.setStudentName(studentName.get(i));
			student.setEmailAddress(studentEmail.get(i));
			student.setNric(studentNric.get(i));
			student.setMobileNumber(studentMobile.get(i));
			students.add(student);
		}

		return students;
	}
	
	private List<Participation> getParticipation(){
		Participation participation = new Participation();
		participation.setStatus(Constants.STATUS_APPLIED);
		participation.setParticipationType(getSelectedType());
		List<Participation> participator = new ArrayList<Participation>();
		participator.add(participation);
		return participator;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}